package com.fzu.mmdbproxyframework.h2Serv;

import java.io.BufferedReader;  
import java.io.IOException;  
import java.io.InputStream;  
import java.io.InputStreamReader;  
import java.sql.SQLException;

import com.alibaba.druid.pool.DruidDataSource;

import ch.ethz.ssh2.Connection;  
import ch.ethz.ssh2.Session;  
import ch.ethz.ssh2.StreamGobbler;  
	  
public class RemoteExecute {  
	  
	 public static void main(String[] args) {  
	          
		 startssh();
	        try {
				System.out.println(getConn().hashCode());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	          
	    }  
	 public static void startssh(){
		  String hostname = "192.168.6.12";  
	        String username = "xiaozhi";  
	        String password = "xiaozhi";  
	        //ָ������������IP��ַ  
	        Connection conn = new Connection(hostname);  
	        Session ssh = null;  
	        try {  
	            //���ӵ�����  
	            conn.connect();  
	            //ʹ���û���������У��  
	            boolean isconn = conn.authenticateWithPassword(username, password);  
	            if(!isconn){  
	                System.out.println("�û����ƻ��������벻��ȷ");  
	            }else{  
	                System.out.println("�Ѿ�����OK");  
	                ssh = conn.openSession();  
	                ssh.execCommand("sh /home/xiaozhi/h2/bin/h2.sh ");
////	                ssh.execCommand("ps");
//	                InputStream  is = new StreamGobbler(ssh.getStdout());  
//	                BufferedReader brs = new BufferedReader(new InputStreamReader(is));  
//	                String line ;
//		            while((line=brs.readLine()) != null){ 
//	                    System.out.println(line);  
//		            }  
//	                is.close();
	            }  
	            ssh.close();  
	            conn.close();  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }  
	 }
	 public static java.sql.Connection getConn() throws SQLException{
		 	DruidDataSource druiddataSource = new DruidDataSource();
			druiddataSource.setDriverClassName("org.h2.Driver");
			druiddataSource.setUsername("sa");
			druiddataSource.setPassword("sa");
			druiddataSource.setUrl("jdbc:h2:tcp://192.168.6.12:9090/mem:test;MODE=MYSQL");//jdbc:mysql://localhost:3306/test
			druiddataSource.setInitialSize(5);//<!-- 初始化连接数量 -->
			druiddataSource.setMaxActive(10);//<!-- 最大并发连接数 -->
//			druiddataSource.setMaxIdle(2);//   <!-- 最大空闲连接数 -->
			druiddataSource.setMinIdle(2);//  <!-- 最小空闲连接数 -->
			druiddataSource.setMaxWait(5000);	// <!-- 配置获取连接等待超时的时间 -->   		
			druiddataSource.setRemoveAbandoned(true);			// <!-- 超过时间限制是否回收 -->
			druiddataSource.setRemoveAbandonedTimeout(2);			//<!-- 超过时间限制多长； -->
			druiddataSource.setLogAbandoned(false);			//<!-- 关闭abanded连接时输出错误日志 --> 
			druiddataSource.setTimeBetweenEvictionRunsMillis(3000);//<!-- 配置间隔多久才进行一次检测，检测需要关闭的空闲连接，单位是毫秒 -->
			druiddataSource.setMinEvictableIdleTimeMillis(300000);//<!-- 配置一个连接在池中最小生存的时间，单位是毫秒 -->
//			druiddataSource.setValidationQuery(" SELECT 1 FROM DUAL");			// <!-- 用来检测连接是否有效的sql，要求是一个查询语句-->		     
//			druiddataSource.setTestWhileIdle(true);// <!-- 申请连接的时候检测 -->
		      
			// 启用监控统计功能  
			druiddataSource.setPoolPreparedStatements(false);
							
			 java.sql.Connection conn = druiddataSource.getConnection().getConnection();
			 System.out.println("conn=="+conn.hashCode());	 
		 
		return conn;
		 
	 }
	  
}  
